﻿namespace TomatoBreak.Infrastructure
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    using RealBuildManager = System.Web.Compilation.BuildManager;

    public class BuildManager : IBuildManager
    {
        private IEnumerable<Assembly> applicationAssemblies;
        private IEnumerable<Assembly> privateAssemblies;

        public IEnumerable<Assembly> PrivateAssemblies
        {
            get
            {
                return privateAssemblies ?? (privateAssemblies = RealBuildManager.GetReferencedAssemblies()
                                                                                 .Cast<Assembly>()
                                                                                 .Where(assembly => !assembly.GlobalAssemblyCache)
                                                                                 .ToList());
            }
        }

        public IEnumerable<Assembly> ApplicationAssemblies
        {
            get { return applicationAssemblies ?? (applicationAssemblies = GenrateApplicationAssemblyList()); }
        }

        private IEnumerable<Assembly> GenrateApplicationAssemblyList()
        {
            var assemblies = PrivateAssemblies.Where(assembly => assembly.GetName().Name.StartsWith("TomatoBreak", StringComparison.Ordinal)).ToList();

            return assemblies;
        }
    }
}